package com.lczc.soa.controller;

import com.lczc.soa.domain.User;
import com.lczc.soa.service.MailService;
import com.lczc.soa.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Random;

@Controller
public class MailController {
    @Autowired
    private MailService mailService;

    @Autowired
    private UserService userService;

    private String code;

    @RequestMapping("getCheckCode")
    @ResponseBody
    public String getCheckCode(String email){
        String checkCode = String.valueOf(new Random().nextInt(899999) + 100000);
        String message = "您的注册验证码为："+checkCode;
        code = checkCode;
        try {
            mailService.sendSimpleMail(email, "注册验证码", message);
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
        return checkCode;
    }

    @RequestMapping("test")
    @ResponseBody
    public String getValue(@RequestParam("username") String username,
                           @RequestParam("email") String email,
                           @RequestParam("password") String password,
                           @RequestParam("repassword") String repassword,
                           @RequestParam("checkCode") String checkCode, Model model){
        System.out.println(username + " , " + email + " , " + password + " , " + checkCode);
        User usercheck = userService.findUserByName(username);
        String msgg = null;
        if (StringUtils.isBlank(username)) {
            msgg = "用户名不能为空";
            //model.addAttribute("msgg", msgg);
            return msgg;
        } else if(usercheck != null){
            msgg = "用户名已存在";
            //model.addAttribute("msgg", msgg);
            return msgg;
        } else if (StringUtils.isBlank(password)) {
            msgg = "密码不能为空";
            //model.addAttribute("msgg", msgg);
            return msgg;
        } else if (StringUtils.isBlank(repassword) || !repassword.equals(password)) {
            msgg = "两次密码输入不一致";
            //model.addAttribute("msgg", msgg);
            return msgg;
        } else if (StringUtils.isBlank(checkCode) || !code.equals(checkCode)){
            msgg = "验证码错误";
            //model.addAttribute("msgg", msgg);
            return msgg;
        } else {
            User user = new User();
            user.setUserName(username);
            user.setPassword(password);
            user.setLinkWay(email);
            int updated = userService.saveUser(user);
//            msgg = "注册成功";
//            model.addAttribute("msgg", msgg);
            return "ok";
        }
    }

    @RequestMapping("mail")
    public String toMailPage(){
        return "mail";
    }
}
